package com.example.exam.service.impl;

import com.example.exam.entity.ExamPaper;
import com.example.exam.entity.ExamPaperAnswer;
import com.example.exam.entity.Question;
import com.example.exam.entity.QuestionAnswer;
import com.example.exam.service.ExamAnswerDetailService;
import com.example.exam.service.ExamPaperAnswerService;
import com.example.exam.service.ExamPaperService;
import com.example.exam.vo.ExamAnswerDetailVO;
import com.example.exam.vo.QuestionDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shishuqian
 * date 2021/5/28
 * time 9:20
 **/
@Service
public class ExamAnswerDetailServiceImpl implements ExamAnswerDetailService {

    @Autowired
    private ExamPaperAnswerService examPaperAnswerService;

    @Autowired
    private ExamPaperService examPaperService;

    @Override
    public ExamAnswerDetailVO getExamAnswerDetail(Integer examAnswerId) throws RuntimeException{
        //取出答题卷
        ExamPaperAnswer examPaperAnswer = examPaperAnswerService.findById(examAnswerId);
        //试卷id
        Integer examPaperId = examPaperAnswer.getExamPaperId();
        //取出试卷(包含的题目已排序)
        ExamPaper examPaper = examPaperService.findById(examPaperId);
        //试卷包含的问题
        List<Question> questionList = examPaper.getQuestionList();
        //问题的作答
        List<QuestionAnswer> questionAnswerList = examPaperAnswer.getQuestionAnswerList();

        ExamAnswerDetailVO examAnswerDetailVO = new ExamAnswerDetailVO();

        List<QuestionDetailVO> questionDetailVOList = new ArrayList<>();

        //遍历试卷中的问题
        for (Question question:questionList){
            QuestionDetailVO vo = new QuestionDetailVO();

            vo.setContent(question.getContent());
            vo.setQuestionType(question.getQuestionType());
            vo.setQuestionScore(question.getScore());
            vo.setRowNum(question.getRowNum());

            vo.setUserScore(0.0);
            vo.setUserAnswer("");
            vo.setDoRight(false);

            for (QuestionAnswer questionAnswer:questionAnswerList){
                if (question.getId().equals(questionAnswer.getQuestionId())){
                    vo.setUserScore(questionAnswer.getUserScore());
                    vo.setUserAnswer(questionAnswer.getUserAnswer());
                    vo.setDoRight(questionAnswer.getDoRight());
                }
            }

            vo.setCorrect(question.getCorrect());
            vo.setAnswerInfo(question.getAnswerInfo());
            vo.setChoiceList(question.getChoiceList());

            //加入voList
            questionDetailVOList.add(vo);
        }
        examAnswerDetailVO.setPaperName(examPaperAnswer.getPaperName());
        examAnswerDetailVO.setDescription(examPaper.getDescription());
        examAnswerDetailVO.setPaperScore(examPaperAnswer.getPaperScore());
        examAnswerDetailVO.setSystemScore(examPaperAnswer.getSystemScore());
        examAnswerDetailVO.setTeacherScore(examPaperAnswer.getTeacherScore());
        examAnswerDetailVO.setFinalScore(examPaperAnswer.getFinalScore());
        examAnswerDetailVO.setQuestionCount(examPaperAnswer.getQuestionCount());
        examAnswerDetailVO.setSystemCorrect(examPaperAnswer.getSystemCorrect());
        examAnswerDetailVO.setTeacherCorrect(examPaperAnswer.getTeacherCorrect());
        examAnswerDetailVO.setCreateTime(examPaperAnswer.getCreateTime());
        examAnswerDetailVO.setDoTime(examPaperAnswer.getDoTime());
        examAnswerDetailVO.setQuestionDetailList(questionDetailVOList);

        return examAnswerDetailVO;
    }
}
